'use strict';

stareal
    .controller("RaidersDetailController", function ($scope, $stateParams,$sce, $api, $state, $alert, localStorageService,$interval,$window,$lazyLoader) {
        $scope.sharUrl='https://test.djx.m.opentickets.cn/?&#/'; // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致'
        $scope.id = $stateParams.id;
        if($stateParams.app==1){
            $scope.BackStatus=true;
            $scope.dowload_appStatus=true;
        }
        $scope.closedDowload=function(){
            $scope.dowload_appStatus=false;
        }
        // console.log($scope.id);
        $scope.viewfavorText=" + 关注";
        $scope.zanBtnStatus=false;
        $scope.user = localStorageService.get("user");
        //获取详情
        var h=$(window).height()-1.35*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        $scope.tokentrue=false;
        $scope.getShare=function(){
            $api.get("app/share/getSignature", {url: window.location.href.split('#')[0]})
                .then(function (ret) {
                    if (ret) {
                        var data = ret.data;
                        wx.config({
                            debug: false,
                            appId: data.appid,
                            timestamp: data.timestamp,
                            nonceStr: data.nonceStr,
                            signature: data.signature,
                            jsApiList: [
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                // 'onMenuShareWeibo',
                                'onMenuShareQZone'
                            ]
                        });
                        wx.ready(function () {
                            //分享到朋友圈    title: $scope.good.title, // 分享标题
                            wx.onMenuShareTimeline({
                                title: $scope.sContent.title, // 分享标题
                                desc: '我在独角秀分享了一篇不错的攻略！', // 分享描述
                                link: $scope.sharUrl+'main/raidersdetail/id/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.sContent.picture.split(',')[0], // 分享图标
                                success: function () {
                                    // 用户确认分享后执行的回调函数
                                    //alert('你好');
                                   // $alert.show("分享成功");
                                },
                                cancel: function () {
                                    // 用户取消分享后执行的回调函数
                                    // alert('你好....');
                                }
                            });
                            //分享给朋友
                            wx.onMenuShareAppMessage({
                                title: $scope.sContent.title, // 分享标题
                                desc: '我在独角秀分享了一篇不错的攻略！', // 分享描述
                                link: $scope.sharUrl+'main/raidersdetail/id/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.sContent.picture.split(',')[0], // 分享图标
                                type: '', // 分享类型,music、video或link，不填默认为link
                                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                                success: function () {
// 用户确认分享后执行的回调函数
                                    //$alert.show("分享成功");
                                    // alert('你好....');
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQQ({
                                title: $scope.sContent.title, // 分享标题
                                desc: '我在独角秀分享了一篇不错的攻略！', // 分享描述
                                link: $scope.sharUrl+'main/raidersdetail/id/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.sContent.picture.split(',')[0], // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                  //  $alert.show("分享成功");
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                            wx.onMenuShareQZone({
                                title: $scope.sContent.title, // 分享标题
                                desc: '我在独角秀分享了一篇不错的攻略！', // 分享描述
                                link: $scope.sharUrl+'main/raidersdetail/id/'+$stateParams.id+'/1', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                imgUrl: $scope.sContent.picture.split(',')[0], // 分享图标
                                success: function () {
// 用户确认分享后执行的回调函数
                                  //  $alert.show("分享成功");
                                },
                                cancel: function () {
// 用户取消分享后执行的回调函数
                                }
                            });
                        });
                        wx.error(function (res) {
                            //console.log(res);
                            //alert("微信分享接口配置失败");
                        });
                    }
                })
        }
        $api.get("app/celebritys/findById",{id:$stateParams.id}, true)
            .then(function(ret) {
                var sContent = ret.data;
                var newCOntents='';
                sContent.content= $sce.trustAsHtml(sContent.content);
                $scope.sContent = sContent;
                var classifyliId=sContent.classifyid; //分类id
                var useId=sContent.user_id;
                if($scope.sContent.favor){ //是否收藏
                    $scope.cangsBtnStatus=true;
                }else{
                    $scope.cangsBtnStatus=false;
                }
                if($scope.sContent.likes){ //是否点赞
                    $scope.zanBtnStatus=true;
                }else{
                    $scope.zanBtnStatus=false;
                }
                if($scope.sContent.top){ //是否顶
                    $scope.topBtnStatus=true;
                }else{
                    $scope.topBtnStatus=false;
                }
                $scope.getShare();
                // if(localStorageService.get('token')!=''){  //app/view/otherscenter
                //     $scope.tokentrue=true;
                //     $api.get("app/view/otherscenter",{authorid:useId}, true).then(function (ret) {
                //         var  othersInfo=ret.data;
                //         // console.log(othersInfo);
                //         $scope.othersInfo=othersInfo;
                //
                //     },function(err){
                //         $alert.show(err);
                //     })
                // }
            })
        $scope.goods = new $lazyLoader("app/viewcomment/goodComments", {
            good_id:$stateParams.id,
            type:4
        },true);
        //观演记收藏
        $scope.viewCollection=function(){
            var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }

            if(!$scope.cangsBtnStatus){
                $api.post("app/view/collection",{viewid:$stateParams.id,type:4},true)
                    .then(function (ret) {
                        if(ret.retCode==0){
                            $alert.show('收藏成功！')
                            $scope.cangsBtnStatus=true;
                        }
                    },function(err){
                        $alert.show(err);
                    })
            }else{
                $api.post("app/view/collection",{viewid:$stateParams.id,type:4},true)
                    .then(function (ret) {
                        if(ret.retCode==0){
                            $alert.show('取消收藏成功！')
                            $scope.cangsBtnStatus=false;
                        }
                    },function(err){
                        $alert.show(err);
                    })
            }
        }
        //独家攻略 点赞  app/view/likesview
        $scope.viewzanstion=function(){
            var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
            if(!$scope.zanBtnStatus){
                $api.post("app/view/likesview",{viewid:$stateParams.id,type:6},true)
                    .then(function (ret) {
                        if(ret.retCode==0){
                            $alert.show('点赞成功！')
                            $scope.zanBtnStatus=true;
                        }

                    },function(err){
                        $alert.show(err);
                    })
            }else{
                $api.post("app/view/likesview",{viewid:$stateParams.id,type:6},true)
                    .then(function (ret) {
                        if(ret.retCode==0){
                            $alert.show('取消点赞！')
                            $scope.zanBtnStatus=false;
                        }

                    },function(err){
                        $alert.show(err);
                    })
            }

        }
        //点赞
        $scope.IsPraiseView = function (commentid,Index,num,from,event){  //tiao
            var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
            event.stopPropagation()//阻止冒泡
            $api.post("app/viewcomment/praise", {comment_id:commentid,type:4}, true)
                .then(function (ret) {
                    if(from=='detail'){//详情页
                        $scope.goods.items[Index].is_praise = ! $scope.goods.items[Index].is_praise;
                        $scope.goods.items[Index].like = parseInt(ret.praise)+num;
                    }
                },function (err) {
                    $alert.show(err)
                })
        }
        //弹出编辑框
        $scope.tanViewCommen=function(){
            var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            if(isbind!=1&&openID!=''){
                localStorageService.set('cleartoken',token);
                location.href = "#/main/register/"+encodeURIComponent(rs);
                localStorageService.set('token','');
                return false
            }
            if (!localStorageService.get('token')) {
                // $state.go("main.login",{})
                // return false;
                var  rs = "main.raidersdetail-" + JSON.stringify({id: $stateParams.id});
                var ua = window.navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {//判断是否是微信浏览器
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state=" + encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }

                return false;
            }
            $("#viewComments_send").show();
        }
        //取消观演记编辑
        $scope.cancelviewCommment=function(){
            $("#editComment").val("");
            $("#viewComments_send").hide();
        }
        //确定发表
        $scope.sendviewCommment=function(){
            if($("#editComment").val()==''){
                $alert.show("请填写发表的内容！");
                return false;
            }
            $api.post("app/viewcomment/create",{
                good_id:$stateParams.id,
                content:$("#editComment").val(),
                type:4
                // star:scor
            },true).then(function (ret) {
                $alert.show("发表成功！");
                location.reload();
            },function(err){
                $alert.show(err);
            })

        }
    });